﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Design;

namespace Jemgine.EffectEditor
{
	public class PathEditor : System.Drawing.Design.UITypeEditor
    {

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null || context.Instance == null)
                return base.GetEditStyle(context);

            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context == null || context.Instance == null || provider == null)
                return value;

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Exe Files (*.exe)|*.exe";
            dlg.CheckFileExists = true;
            dlg.Title = "Select the engine executable.";

            string filename = (string)value;
            if (!System.IO.File.Exists(filename))
                filename = null;

            dlg.FileName = filename;
            if (dlg.ShowDialog() == DialogResult.OK)
                filename = dlg.FileName;
            return filename;
        }
    }

    public class FolderEditor : System.Drawing.Design.UITypeEditor
    {

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null || context.Instance == null)
                return base.GetEditStyle(context);

            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context == null || context.Instance == null || provider == null)
                return value;

            FolderBrowserDialog dlg = new FolderBrowserDialog();

            string folder = (string)value;
            if (!System.IO.Directory.Exists(folder)) folder = null;
            dlg.SelectedPath = folder;

            if (dlg.ShowDialog() == DialogResult.OK)
                folder = dlg.SelectedPath;
            return folder;
        }
    }

	public class Config
	{
        [Microsoft.Xna.Framework.Content.ContentSerializerIgnore]
        public List<System.Reflection.Assembly> _loadedAssemblies = new List<System.Reflection.Assembly>();

	    [ContentSerializer(Optional = true), System.ComponentModel.Editor(typeof(FolderEditor), typeof(UITypeEditor)), Category("Engine")]
        public String EngineFolder { get; set; }

        [ContentSerializer(Optional = true), Category("Engine")]
        public String Episode { get; set; }

		public Config()
		{
            EngineFolder = "";
            Episode = "";
		}
	}
}
